package beans;

import entities.Composizione;
import controllers.ComposizioneFacade;
import java.io.Serializable;
import java.util.ResourceBundle;
import javax.annotation.PostConstruct;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.inject.Named;
import javax.inject.Inject;
import javax.faces.view.ViewScoped;

@Named(value = "composizioneController")
@ViewScoped
public class ComposizioneController extends AbstractController<Composizione> implements Serializable {

    @Inject
    private ComposizioneFacade ejbFacade;

    public ComposizioneController() {
        super(Composizione.class);
    }

    @PostConstruct
    public void init() {
        super.setFacade(ejbFacade);
    }
    
    @Override
     public void saveNew(ActionEvent event) {
        super.selected.setIdComposizione(Integer.MAX_VALUE);
        super.selected.setQuantita(1);
        super.saveNew(event);
    }
     
     
      @Override
    public void delete(ActionEvent event) {
        String elemento = (String) FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("elemDel");
        String viaggio = (String) FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("travDel");
        System.out.println("elemento = "+ elemento+" viaggio = "+viaggio);
        super.selected = this.ejbFacade.findByComposizione(elemento, viaggio);
        String msg = ResourceBundle.getBundle("/MyBundle").getString(itemClass.getSimpleName() + "Deleted");
        persist(PersistAction.DELETE, msg);
        if (!isValidationFailed()) {
            selected = null; // Remove selection
            items = null; // Invalidate list of items to trigger re-query.
        }
    }


}
